package com.coincalf.chan.websocket;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.tio.server.ServerGroupContext;
import org.tio.websocket.server.WsServerStarter;

/**
 * @author tanyaowu
 * 2017年6月28日 下午5:34:04
 */
@Component
public class ChannelWebsocketStarter implements CommandLineRunner {

    private WsServerStarter wsServerStarter;

    private static ServerGroupContext serverGroupContext;

    /**
     * @return the serverGroupContext
     */
    public static ServerGroupContext getServerGroupContext() {
        return serverGroupContext;
    }

    public WsServerStarter getWsServerStarter() {
        return wsServerStarter;
    }

    @Override
    public void run(String... args) throws Exception {
        wsServerStarter = new WsServerStarter(ChannelServerConfig.SERVER_PORT, ChannelWsMsgHandler.INSTANCE);

        serverGroupContext = wsServerStarter.getServerGroupContext();
        serverGroupContext.setName(ChannelServerConfig.PROTOCOL_NAME);
        serverGroupContext.setServerAioListener(ChannelServerAioListener.INSTANCE);

        //设置ip统计时间段
        serverGroupContext.ipStats.addDurations(ChannelServerConfig.IpStatDuration.IPSTAT_DURATIONS);
        //设置ip监控
        //serverGroupContext.setIpStatListener(ShowcaseIpStatListener.me);
        //设置心跳超时时间
        serverGroupContext.setHeartbeatTimeout(ChannelServerConfig.HEARTBEAT_TIMEOUT);
        wsServerStarter.start();
    }

}
